package myky.com.doctorapp.fragment


import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Message
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.github.nukc.stateview.Constant
import com.google.gson.Gson
import kotlinx.android.synthetic.main.fragment_news_sub.*
import myky.com.doctorapp.R
import myky.com.doctorapp.adapter.DoctorArtcalAdapter
import myky.com.doctorapp.api.Address
import myky.com.doctorapp.entity.HealthEntity
import myky.com.doctorapp.utils.Constants
import myky.com.doctorapp.utils.OkHttpUtils.OkHttpManager
import myky.com.doctorapp.utils.RecyclerUtils
import myky.com.doctorapp.utils.ToastUtils
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


/**
 * Title:
 * Description: TODO
 * @author xiangyao
 * date 2019/1/14
 */
class DoctorLectureSubFragment  : Fragment {

    constructor(){

    }
    var canLoadMore: Boolean = false
    var mTagid: Int = -1
    lateinit var articleList: MutableList<HealthEntity.PayLoadBean.ArticleListBean>

    lateinit var doctorArtcalAdapter: DoctorArtcalAdapter


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        var rootView = inflater.inflate(R.layout.fragment_doctor_lecture_sub, container, false)

        EventBus.getDefault().register(this)

        mTagid= arguments.getInt(Constants.TAGID)
        return rootView
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        articleList = ArrayList()

        artcalListView.layoutManager = LinearLayoutManager(activity)

        doctorArtcalAdapter = DoctorArtcalAdapter(articleList)

        artcalListView.adapter = doctorArtcalAdapter

        doctorArtcalAdapter.setOnLoadMoreListener {
            if (canLoadMore) {
                getMoreHealthList(mTagid, RecyclerUtils.getSortByItem(10))
            }
        }

    }

    override fun onHiddenChanged(hidden: Boolean) {
        if (hidden) {
            loadingLayout.visibility = View.VISIBLE
        } else {
            //获取健康学堂列表第一次查看0-9条信息
            getHealthList(mTagid, RecyclerUtils.getSortByFrist(0, 9))
        }
    }
    override fun onDestroyView() {
        super.onDestroyView()
        EventBus.getDefault().unregister(this)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun scrollToFirst(message: Message) {
        if (message.what == Constant.SCROLLTOFIST) {
            if (articleList.size > 0) {
                artcalListView.smoothScrollToPosition(0)
            }
        }
    }


    fun getMoreHealthList(tagId: Int, sortBy: String) {
        val url = Address.HEALTHLECTUREURL + tagId + "/" + sortBy
        val instance = OkHttpManager.getInstance(activity)


        instance.get(url, object : OkHttpManager.HttpCallBack {
            override fun onFailure(errorMsg: String?) {

                ToastUtils.show("网络异常")
            }

            override fun onResponse(jsonObject: String?, type: Long) {
                val gson = Gson()
                val healthBean = gson.fromJson<HealthEntity>(jsonObject, HealthEntity::class.java)
                val articleLists = healthBean.payLoad.articleList
                if (articleLists == null) {
                    doctorArtcalAdapter.loadMoreEnd()
                } else {
                    articleList.addAll(articleLists)
                    doctorArtcalAdapter.loadMoreComplete()
                }
                doctorArtcalAdapter.notifyDataSetChanged()

            }
        })

    }

    //展示医生讲堂列表
    private fun getHealthList(tagId: Int, sortBy: String) {

        val url = Address.HEALTHLECTUREURL + tagId + "/" + sortBy

        OkHttpManager.getInstance(activity).get(url, object : OkHttpManager.HttpCallBack {


            override fun onFailure(errorMsg: String) {
            }

            override fun onResponse(jsonObject: String, type: Long) {
                try {
                    val gson = Gson()
                    val healthBean = gson.fromJson<HealthEntity>(jsonObject, HealthEntity::class.java)
                    val articleLists = healthBean.payLoad.articleList
                    if (articleLists != null && articleLists.size > 0) {
                        canLoadMore = true
                        doctorArtcalAdapter.loadMoreComplete()
                        articleList.addAll(articleLists)
                        doctorArtcalAdapter.notifyDataSetChanged()
                    } else {
                        doctorArtcalAdapter.loadMoreComplete()
                    }
                    loadingLayout.postDelayed({
                        if (articleList.size > 0) {
                            loadingLayout.visibility = View.GONE
                        }
                    }, 500)
                    if (articleList.size > 0) {
                        artcalListView.smoothScrollToPosition(0)
                    }
                    doctorArtcalAdapter.notifyDataSetChanged()
                } catch (e: Exception) {

                }

            }
        })


    }


}
